package com.huanghuai.retail.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.dto.EmployeeDto;
import com.huanghuai.retail.entity.Employee;
import com.huanghuai.retail.mapper.EmployeeMapper;
import com.huanghuai.retail.service.EmployeeServer;
import com.huanghuai.retail.utils.RetailConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServerImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeServer {

    /**
     * 新增员工，已经写了基本信息和员工类型，该功能由管理员操作
     * 自动生成账号和密码并返回
     * @param employee
     * @return
     */
    @Override
    public EmployeeDto addEmployee(Employee employee) {
        //判断传入信息是否完备
        if (employee.getCategoryId() == null || employee.getSex() == null ||
            employee.getPhone() == null || employee.getName() == null)
            return null;

        //生成账号密码并保存数据库
        employee.setUserName(RandomUtil.randomString(10));
        employee.setPassword(RandomUtil.randomString(10));
        employee.setStatus(1);
        save(employee);

        EmployeeDto employeeDto = new EmployeeDto();
        BeanUtils.copyProperties(employee, employeeDto);
        employeeDto.setCategoryName(RetailConstant.getConst("EMPLOYEE_"+employee.getCategoryId()));
        return employeeDto;
    }

    /**
     * 使用分页查询查询所有，并封装到dto中
     * @return
     */
    @Override
    public Page<EmployeeDto> getEmployeesDtoPage(int page, int pageSize, String name, Integer categoryId) {
        Page<Employee> employeePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //条件匹配
        if (StringUtils.isNotBlank(name))
            employeeLambdaQueryWrapper.like(Employee::getName, name);
        if (categoryId != null)
            employeeLambdaQueryWrapper.eq(Employee::getCategoryId, categoryId);
        //执行分页查询
        this.page(employeePage, employeeLambdaQueryWrapper);

        //判空
        if (employeePage == null)
            return null;


        //封装到dto中
        List<Employee> employeeList = employeePage.getRecords();
        List<EmployeeDto> employeeDtoList = new ArrayList<>();
        for (int i = 0; i < employeeList.size(); i ++){
            Employee employee = employeeList.get(i);
            EmployeeDto employeeDto = new EmployeeDto();
            BeanUtils.copyProperties(employee, employeeDto);
            String typeName = "EMPLOYEE_" + employee.getCategoryId();
            employeeDto.setCategoryName(RetailConstant.getConst(typeName));
            employeeDtoList.add(employeeDto);
        }

        Page<EmployeeDto> employeeDtoPage = new Page<>();
        BeanUtils.copyProperties(employeePage, employeeDtoPage, "records");

        employeeDtoPage.setRecords(employeeDtoList);

        return employeeDtoPage;
    }

    @Override
    public R<List<EmployeeDto>> getEmployeeDtoList(Integer typeId) {
        String typeName = "EMPLOYEE_" + typeId;
        typeName = RetailConstant.getConst(typeName);
        if (typeName == null)
            return R.error("没有此类型的人员");
        //查询对应人员
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getCategoryId, typeId);
        List<Employee> employeeList = list(lambdaQueryWrapper);
        if (employeeList == null || employeeList.isEmpty())
                return R.error("查询不到信息");

        List<EmployeeDto> employeeDtoList = new ArrayList<>(employeeList.size());
        //封装到dto
        for (int i = 0; i < employeeList.size(); i ++){
            Employee employee = employeeList.get(i);
            EmployeeDto employeeDto = new EmployeeDto();
            BeanUtil.copyProperties(employee, employeeDto);
            employeeDto.setCategoryName(typeName);
            employeeDtoList.add(employeeDto);
        }
        return R.success(employeeDtoList);
    }
}
